package com.baidu.speech.messageactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.aip.asrwakeup3.core.mini.ActivityMiniRecog;
import com.baidu.speech.asr.SpeechConstant;

import org.json.JSONException;
import org.json.JSONObject;

public class SoundInputActivity extends ActivityMiniRecog {
    private static final String TAG = "SoundInputActivity";
    Button mBtnStart;
    Button mBtnStop;
    TextView mTvArithmetic;
    TextView mTvResult;
    private String bestResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound_input);
        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SoundInputActivity.this.start();
            }
        });
        mBtnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SoundInputActivity.this.stop();

            }
        });
    }

    private void initView() {
        mBtnStart = findViewById(R.id.btn_start);
        mBtnStop = findViewById(R.id.btn_stop);
        mTvArithmetic = findViewById(R.id.arithmetic);
        mTvResult = findViewById(R.id.tvResult);
    }
    @Override
    public void onEvent(String name, String params, byte[] data, int offset, int length) {
        String logTxt = "name: " + name;


        if (params != null && !params.isEmpty()) {
            logTxt += " ;params :" + params;
        }
        if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
            if (params != null && params.contains("\"nlu_result\"")) {
                if (length > 0 && data.length > 0) {
                    logTxt += ", 语义解析结果：" + new String(data, offset, length);
                }
            }
        } else if (data != null) {
            logTxt += " ;data length=" + data.length;
        }

        Log.e(TAG, "onEvent: "+logTxt );
        if(name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)){
            JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(params);
                String result_type = jsonObject.getString("result_type");
                if(result_type.equals("final_result")){
                    bestResult = jsonObject.getString("best_result");
                    if(!bestResult.isEmpty()) {
                        mTvResult.setText(bestResult);
                        Intent intent = new Intent();
                        intent.putExtra("data",bestResult);
                        setResult(101,intent);
                        SoundInputActivity.this.finish();
                    }else{

                    }
                    Log.e(TAG, "onEvent: "+ bestResult);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
